
import { debounce } from "../../utils/util";
import {getDeptList} from "../../api/sys";
const app = getApp();
const globalData = app.globalData;
const {curDept} = app.globalData;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    defaultIndex: {
      type: Number,
      value: 0,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    curDept,
    show: false,
    showEl: false,
    // 部门列表
    deptList: [],
  },
  lifetimes: {
    /**
     * 生命周期函数--在组件实例进入页面节点树时执行
     */
    attached: function() {
      this.getDeptList();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onShow() {
      this.getDeptList();
      this.setData({
        show: true,
        showEl: true,
      });
    },
    onClose() {
      const that = this;
      that.setData({
        show: false,
      });
      const debouncedHide = debounce(() => {
        that.setData({
          showEl: false,
        });
      }, 500);
      debouncedHide();
    },

    onConfirm(e) {
      const {value} = e.detail;
      this.setData({
        curDept: value,
      });
      globalData.curDept = value;
      this.onClose();
      this.triggerEvent("onConfirm",e);
    },
    setIndexes(indexes) {
      this.pickerComponent = this.selectComponent("#pickerComponent");
      this.pickerComponent.setIndexes(indexes);
    },
    getDeptList() {
      if (this.data.deptList && this.data.deptList.length > 0) {
        return;
      }
      getDeptList({
        data: {},
        callBack: (res) => {
          this.setData({
            deptList: res.data.filter(item => item.parentId == 100).map(item => {
              return {'value': item.deptId, 'text': item.deptName}
            })
          })
        },
      })
    },
  },
});
